package com.basics.common.utils;


import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    public static String YYYY = "yyyy";
    public static String YYYY_MM = "yyyy-MM";
    public static String YYYY_MM_DD = "yyyy-MM-dd";
    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    private static String[] parsePatterns = new String[]{"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * new SimpleDateFormat("yyyy-MM-dd");
     */
    public final static SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     */
    public final static SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public DateUtils() {
    }

    public static Date getNowDate() {
        return new Date();
    }

    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date) {
        return (new SimpleDateFormat(format)).format(date);
    }

    public static final Date dateTime(final String format, final String ts) {
        try {
            return (new SimpleDateFormat(format)).parse(ts);
        } catch (ParseException var3) {
            throw new RuntimeException(var3);
        }
    }

    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        } else {
            try {
                return parseDate(str.toString(), parsePatterns);
            } catch (ParseException var2) {
                return null;
            }
        }
    }

    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 86400000L;
        long nh = 3600000L;
        long nm = 60000L;
        long diff = endDate.getTime() - nowDate.getTime();
        long day = diff / nd;
        long hour = diff % nd / nh;
        long min = diff % nd % nh / nm;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    public static Date addDateHour(Date date, int hour) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("front:" + format.format(date));
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(10, hour);
        date = cal.getTime();
        System.out.println("after:" + format.format(date));
        return cal.getTime();
    }

    public static Date addDateMinus(Date date, int minus) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, minus);
        return cal.getTime();
    }

    public static Date addDateDay(Date date, int days) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("front:" + format.format(date));
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(6, days);
        date = cal.getTime();
        System.out.println("after:" + format.format(date));
        return cal.getTime();
    }

    public static Date addDateMonth(Date date, int months) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("front:" + format.format(date));
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(2, months);
        date = cal.getTime();
        System.out.println("after:" + format.format(date));
        return cal.getTime();
    }

    public static long getDaysDatePoor(Date endDate, Date nowDate) {
        long nd = 86400000L;
        long diff = endDate.getTime() - nowDate.getTime();
        return diff / nd;
    }

    public static BigDecimal getHoursDatePoor(Date endDate, Date nowDate) {
        long nd = 3600000L;
        long diff = endDate.getTime() - nowDate.getTime();
        return BigDecimal.valueOf(diff).divide(BigDecimal.valueOf(nd), 8, 3);
    }

    public static Date getTodayHour(int hours) {
        Calendar c = Calendar.getInstance();
        c.set(11, hours);
        c.set(12, 0);
        c.set(13, 0);
        return c.getTime();
    }

    public static Date getBeginDayOfWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            int dayofweek = cal.get(7);
            if (dayofweek == 1) {
                dayofweek += 7;
            }

            cal.add(5, 2 - dayofweek);
            return cal.getTime();
        }
    }

    public static Date getEndDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfWeek());
        cal.add(7, 6);
        return cal.getTime();
    }

    public static String getNowWeekStartDay() {
        SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-dd  00:00:00");
        return startSdf.format(getBeginDayOfWeek().getTime());
    }

    public static String getNowWeekEndDay() {
        SimpleDateFormat endSdf = new SimpleDateFormat("yyyy-MM-dd  23:59:59");
        return endSdf.format(getEndDayOfWeek().getTime());
    }

    public static String getWeekStartDay() {
        Calendar c = Calendar.getInstance();
        c.set(7, 2);
        c.add(5, -7);
        SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-dd  00:00:00");
        return startSdf.format(c.getTime());
    }

    public static String getWeekEndDay() {
        Calendar c = Calendar.getInstance();
        c.set(7, 2);
        c.add(5, -1);
        SimpleDateFormat endSdf = new SimpleDateFormat("yyyy-MM-dd  23:59:59");
        return endSdf.format(c.getTime());
    }

    public static Integer getweek(String mydata) {
        Integer week = null;

        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setFirstDayOfWeek(2);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            Date time = sdf.parse(mydata);
            cal.setTime(time);
            int dayWeek = cal.get(7);
            if (1 == dayWeek) {
                cal.add(5, -1);
            }

            cal.setFirstDayOfWeek(2);
            int day = cal.get(7);
            cal.add(5, cal.getFirstDayOfWeek() - day);
            String date1 = sdf.format(cal.getTime());
            Date time1 = sdf.parse(date1);
            calendar.setTime(time1);
            int week1 = calendar.get(3);
            cal.add(5, 6);
            String date2 = sdf.format(cal.getTime());
            Date time2 = sdf.parse(date2);
            calendar.setTime(time2);
            int week2 = calendar.get(3);
            week = week1 >= week2 ? week2 : week1;
            return week;
        } catch (Exception var14) {
            var14.printStackTrace();
            throw new RuntimeException(var14);
        }
    }

    public static int getTodayHour() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(11);
    }


    //得到月份第一天
    public static Date getDateFirstDay(String year, String month) throws ParseException {

        //year="2018" month="2"
        Calendar calendar = Calendar.getInstance();
        // 设置时间,当前时间不用设置
        calendar.set(Calendar.YEAR, Integer.parseInt(year));
        calendar.set(Calendar.MONTH, Integer.parseInt(month)-1);

        // System.out.println(calendar.getTime());

        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return simpleDateFormat3.parse(simpleDateFormat1.format(calendar.getTime())+" 00:00:00");
    }

    //得到月份最后一天
    public static Date getDateLastDay(String year, String month) throws ParseException {

        //year="2018" month="2"
        Calendar calendar = Calendar.getInstance();
        // 设置时间,当前时间不用设置
        calendar.set(Calendar.YEAR, Integer.parseInt(year));
        calendar.set(Calendar.MONTH, Integer.parseInt(month)-1);

        // System.out.println(calendar.getTime());

        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        return simpleDateFormat3.parse(simpleDateFormat1.format(calendar.getTime())+" 23:59:59");
    }

    /**
     * 获得当月1号零时零分零秒
     * @return
     */
    public static Date initDateByMonth(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 得到今天之中的零点时间
     * @return
     * @throws ParseException
     */
    public static Date getZeroDate() throws ParseException{
        SimpleDateFormat format = null;
        Date date = new Date();
        format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        String rtnYes = format.format(date);
        return format.parse(rtnYes);
    }

    /**
     * 得到时间之中的零点时间
     * @return
     * @throws ParseException
     */
    public static Date getZeroByDate(Date date) throws ParseException{
        SimpleDateFormat format = null;
        format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        String rtnYes = format.format(date);
        return format.parse(rtnYes);
    }

    /**
     * 得到今天之中的的23点59分59秒
     * @return
     * @throws ParseException
     */
    public static Date getLastDate() throws ParseException{
        SimpleDateFormat format = null;
        Date date = new Date();
        format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        String rtnYes = format.format(date);
        format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.parse(rtnYes);
    }

    public static Date getLastDate(Date date) throws ParseException{
        SimpleDateFormat format = null;
        format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        String rtnYes = format.format(date);
        format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.parse(rtnYes);
    }

    public static int getWeek(Date date) {

        GregorianCalendar g = new GregorianCalendar();

        g.setTime(date);

        return g.get(Calendar.WEEK_OF_YEAR); //获得周数

    }

    //传入时间戳即可
    public static Date conversionTime(String timeStamp) {
        //yyyy-MM-dd HH:mm:ss 转换的时间格式  可以自定义
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //转换
        return new Date(Long.parseLong(timeStamp));
    }

    public static int getWeek(){
        Calendar calendar;
        calendar = Calendar.getInstance();
        int week = calendar.get(calendar.DAY_OF_WEEK) - 1 ;
        if (week==0) {
            week = 7;
        }
        return week;
    }

    public static Long getTimeStamp(Date date){
        if(date==null){
            return 0L;
        }
        return date.getTime();
    }

    /**
     * 秒，转换为人类可读的时间
     * @param seconds
     * @return
     */
    public static String secondsToRead(int seconds) {
        int h = seconds / 3600;            //小时
        int m = (seconds % 3600) / 60;        //分钟
        int s = (seconds % 3600) % 60;        //秒
        if (h > 0) {
            return h + "小时" + m + "分钟" + s + "秒";
        }
        if (m > 0) {
            return m + "分钟" + s + "秒";
        }
        return s + "秒";
    }

    public static void main(String[] args) {
        System.out.println(addDateMinus(new Date(),-5));
    }
}

